package com.tech.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class DBUtil {
	private static final String resource = "config/mybatis-config.xml";
	private static SqlSession sqlSession = null;

	static {
		try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
			SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(inputStream);
			sqlSession = sqlMapper.openSession(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static SqlSession getSqlSession() {
		return sqlSession;
	}

	public static <T> T getMapper(Class<T> type) {
		return sqlSession.getMapper(type);
	}
}
